package com.example;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;

import com.example.databinding.ActivityDynamicVariablesExampleBinding;
import com.example.databinding.UserItemBinding;

import java.util.ArrayList;
import java.util.List;

public class DynamicVariablesExampleActivity extends AppCompatActivity {

    static class BindingHolder extends RecyclerView.ViewHolder {

        private ViewDataBinding binding;

        public BindingHolder(View view) {
            super(view);
        }

        public BindingHolder(ViewDataBinding binding, View view) {
            super(view);
            this.binding = binding;
        }

        public ViewDataBinding getBinding() {
            return binding;
        }
    }

    static class Adapter extends RecyclerView.Adapter<BindingHolder> {

        private List<ObservableUser> mItems;

        public void setItems(List<ObservableUser> list) {
            this.mItems = list;
            notifyDataSetChanged();
        }

        @Override
        public void onBindViewHolder(BindingHolder holder, int position) {
            final ObservableUser item = mItems.get(position);
            holder.getBinding().setVariable(BR.user, item);
            holder.getBinding().executePendingBindings();
        }

        @Override
        public int getItemCount() {
            return null == mItems ? 0 : mItems.size();
        }

        @Override
        public BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            //　上面三种方式都可以,这里我采用第二种
            // 1
//            UserItemBinding binding = UserItemBinding.inflate(layoutInflater);
            // 2
            UserItemBinding binding = UserItemBinding.inflate(layoutInflater, parent, false);
            // 3
//            UserItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.user_item, parent, false);
            return new BindingHolder(binding, binding.getRoot());
        }
    }

    private Adapter adapter = new Adapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityDynamicVariablesExampleBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_dynamic_variables_example);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));

        List<ObservableUser> list = new ArrayList<>();
        ObservableUser user = new ObservableUser("xx", "yy", 11, "http://img3.redocn.com/tupian/20150929/caiseyuanqiudongtailianjiePPT_4580490.jpg");
        list.add(user);
        user = new ObservableUser("fsf", "fsfs", 11, "http://img3.redocn.com/tupian/20150929/caiseyuanqiudongtailianjiePPT_4580490.jpg");
        list.add(user);
        user = new ObservableUser("ccc", "oo", 11, "http://img3.redocn.com/tupian/20150929/caiseyuanqiudongtailianjiePPT_4580490.jpg");
        list.add(user);
        user = new ObservableUser("yy", "www", 11, "http://img3.redocn.com/tupian/20150929/caiseyuanqiudongtailianjiePPT_4580490.jpg");
        list.add(user);
        adapter.setItems(list);
    }
}
